﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for previewWindow.xaml
    /// </summary>
    public partial class previewWindow : Window
    {
        public previewWindow(String path)
        {
            InitializeComponent();
            int ratio;

            //// Open a Uri and decode a BMP image
            //Uri myUri = new Uri(path, UriKind.RelativeOrAbsolute);
            //BmpBitmapDecoder decoder2 = new BmpBitmapDecoder(myUri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            //BitmapSource bitmapSource2 = decoder2.Frames[0];

            BitmapImage bitmapSource2 = BitmapImageFromFile(path);

            // pengaturan
            if (bitmapSource2.Width < 600 && bitmapSource2.Height < 600)
            {
                this.Width = bitmapSource2.Width;
                this.Height = bitmapSource2.Height;
                image1.Width = bitmapSource2.Width;
                image1.Height = bitmapSource2.Height;
            }
            else if (bitmapSource2.Width > bitmapSource2.Height)
            {
                ratio = (int)((bitmapSource2.Height / bitmapSource2.Width) * 600);
                this.Width = 600;
                this.Height = ratio;
                image1.Width = 600;
                image1.Height = ratio;
            }
            else
            {
                ratio = (int)((bitmapSource2.Width / bitmapSource2.Height) * 600);
                this.Width = ratio;
                this.Height = 600;
                image1.Width = ratio;
                image1.Height = 600;
            }

            // Draw the Image
            image1.Source = bitmapSource2;
            image1.Stretch = Stretch.Uniform;
        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private BitmapImage BitmapImageFromFile(string sFileName)
        {
            // Create source.
            BitmapImage bi = new BitmapImage();
            // Change properties within a BeginInit/EndInit block.
            bi.BeginInit();
            Uri mUri = new Uri("file://" + sFileName, UriKind.RelativeOrAbsolute);
            bi.UriSource = mUri;
            bi.EndInit();
            return bi;
        }

    }
}
